package com.cn.algorithm.leetcode.link;

/*
*
* 反转链表
*
*/
public class ReversedLinkedList {


    public static ListNode reverseList(ListNode head) {

        ListNode tmp = null;
        ListNode prev = null;
        while (head != null) {
            tmp = head.next;
            head.next = prev;
            prev = head;
            head = tmp;
        }
        return prev;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);

        ListNode head2 = new ListNode(2);
        head1.next = head2;
        ListNode head3 = new ListNode(3);
        head2.next = head3;
        System.out.println(reverseList(head1));

    }

    public static class ListNode {
        int var;
        ListNode next;
        public ListNode(int var) {
            this.var = var;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "var=" + var +
                    ", next=" + next +
                    '}';
        }
    }
}
